﻿using System.Collections.Generic;
using Microsoft.Practices.Unity;
using StockholmsStad.Windows.UI.Infrastructure.Interfaces;
using StockholmsStad.Windows.UI.Modules.WatchList.Interfaces;
using StockholmsStad.Windows.UI.Facade;
using StockholmsStad.Windows.UI.Modules.WatchList.Models;
using System.Collections.ObjectModel;
using StockholmsStad.Runtime.Serialization.DataContract;
using StockholmsStad.Runtime.Serialization.DataContract.Enums;

namespace StockholmsStad.Windows.UI.Modules.WatchList.Services
{
    public class WatchListService : IWatchListService
    {
        private ObservableCollection<WatchListPresentationModel> messages;
        private IApplicationService applicationService;

        [Dependency]
        public IApplicationService ApplicationService
        {
            set { applicationService = value; }
        }

        #region IWatchListService Members

        public ObservableCollection<WatchListPresentationModel> GetMessages()
        {
            var user = applicationService.GetCurrentUser();
            var facade = new TicketFacade(user);

            var listOfMessages = facade.GetMessages();

            return CreatePresentationObject(listOfMessages);
        }

        #endregion

        private ObservableCollection<WatchListPresentationModel> CreatePresentationObject(IEnumerable<History> listOfMessages)
        {
            var user = applicationService.GetCurrentUser();
            bool isReadOnly = user.Role == UserRole.ReadOnly ? true : false;

            if (messages == null)
                messages = new ObservableCollection<WatchListPresentationModel>();
            else
                messages.Clear();

            if(listOfMessages == null)
                return messages;

            foreach(var message in listOfMessages)
            {
                var model = new WatchListPresentationModel
                                {
                                    HistoryId = message.HistoryId.Value,
                                    TicketId = message.TicketId,
                                    Date = message.DateTime,
                                    Information = message.Information,
                                    ModifiedByUser = message.ModifiedByUser,
                                    ReadOnly = isReadOnly,
                                };

                model.Noticed += model_Noticed;

                messages.Add(model);
            }

            return messages;
        }

        private void model_Noticed(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<WatchListPresentationModel> e)
        {
            if(e != null && e.Value != null)
            {
                var model = e.Value;
                var user = applicationService.GetCurrentUser();
                var facade = new TicketFacade(user);
                facade.MessageNoticed(model.HistoryId);
                messages.Remove(model);
            }
        }
    }
}
